﻿#include "mainwindow.h"
#include <QAction>
#include <QMenuBar>
#include <QDebug>
#include <QTime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    mediaPlayer = new QMediaPlayer(this);
    videoWidget = new QVideoWidget(this);
    positionSlider = new QSlider(Qt::Horizontal,this);
    volumeSlider = new QSlider(Qt::Horizontal,this);
    playPauseButton = new QPushButton("播放/暂停",this);
    muteButton = new QPushButton("静音",this);
    nextButton = new QPushButton("下一个",this);
    previousButton = new QPushButton("上一个",this);
    rateButton = new QPushButton("1× 倍速",this);
    playlistWidget = new QListWidget(this);
    currentTimeLabel = new QLabel("00:00",this);
    totalTimeLabel = new QLabel("00:00",this);
    videoNameLabel = new QLabel("当前无视频播放中。。。",this);
    currentVideoIndex = -1;

    setupUi();

    connect(mediaPlayer, &QMediaPlayer::positionChanged, this, &MainWindow::updatePosition);
    connect(mediaPlayer, &QMediaPlayer::durationChanged, this, &MainWindow::updateDuration);
    connect(mediaPlayer, &QMediaPlayer::mediaStatusChanged, this, &MainWindow::updateMediaInfo);
}

MainWindow::~MainWindow()
{
    delete mediaPlayer;
    delete videoWidget;
    delete positionSlider;
    delete volumeSlider;
    delete playPauseButton;
    delete muteButton;
    delete nextButton;
    delete previousButton;
    delete rateButton;
    delete playlistWidget;
    delete currentTimeLabel;
    delete totalTimeLabel;
    delete videoNameLabel;
}

void MainWindow::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Space:
        playPause();
        break;
    case Qt::Key_Right:
        mediaPlayer->setPosition(mediaPlayer->position() + 10000); // 快进10秒
        break;
    case Qt::Key_Left:
        mediaPlayer->setPosition(mediaPlayer->position() - 10000); // 倒退10秒
        break;
    default:
        QMainWindow::keyPressEvent(event);
    }
}

void MainWindow::openFolder()
{
    QString folderPath = QFileDialog::getExistingDirectory(this, "打开文件夹", QDir::homePath());
    if (!folderPath.isEmpty()) {
        loadVideos(folderPath);
    }
}

void MainWindow::playPause()
{
    if (mediaPlayer->state() == QMediaPlayer::PlayingState) {
        mediaPlayer->pause();
        playPauseButton->setText("播放");
    } else {
        mediaPlayer->play();
        playPauseButton->setText("暂停");
    }
}

void MainWindow::muteUnmute()
{
    if (mediaPlayer->isMuted()) {
        mediaPlayer->setMuted(false);
        muteButton->setText("静音");
    } else {
        mediaPlayer->setMuted(true);
        muteButton->setText("正常");
    }
}

void MainWindow::changeVolume(int value)
{
     mediaPlayer->setVolume(value);
}

void MainWindow::changePosition(int value)
{
    mediaPlayer->setPosition(value);
}

void MainWindow::updatePosition(qint64 position)
{
    positionSlider->setValue(position);
    QTime currentTime(0, (position / 60000) % 60, (position / 1000) % 60);
    currentTimeLabel->setText(currentTime.toString("mm:ss"));
}

void MainWindow::updateDuration(qint64 duration)
{
    positionSlider->setRange(0, duration);
    QTime totalTime(0, (duration / 60000) % 60, (duration / 1000) % 60);
    totalTimeLabel->setText(totalTime.toString("mm:ss"));
}

void MainWindow::playNext()
{
    if (currentVideoIndex < videoFiles.size() - 1) {
        currentVideoIndex++;
        mediaPlayer->setMedia(QUrl::fromLocalFile(videoFiles[currentVideoIndex]));
        mediaPlayer->play();
    }
}

void MainWindow::playPrevious()
{
    if (currentVideoIndex > 0) {
        currentVideoIndex--;
        mediaPlayer->setMedia(QUrl::fromLocalFile(videoFiles[currentVideoIndex]));
        mediaPlayer->play();
    }
}

void MainWindow::changePlaybackRate()
{
    if (mediaPlayer->playbackRate() == 1.0) {
        mediaPlayer->setPlaybackRate(1.5);
        rateButton->setText("1.5x");
    } else if (mediaPlayer->playbackRate() == 1.5) {
        mediaPlayer->setPlaybackRate(2.0);
        rateButton->setText("2x");
    } else {
        mediaPlayer->setPlaybackRate(1.0);
        rateButton->setText("1x");
    }
}

void MainWindow::updateMediaInfo()
{
    if (mediaPlayer->mediaStatus() == QMediaPlayer::LoadedMedia) {
        videoNameLabel->setText(videoFiles[currentVideoIndex]);
    }
}

void MainWindow::setupUi()
{
    QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    setCentralWidget(centralWidget);

    QHBoxLayout *controlLayout = new QHBoxLayout();
    controlLayout->addWidget(playPauseButton);
    controlLayout->addWidget(previousButton);
    controlLayout->addWidget(nextButton);
    controlLayout->addWidget(muteButton);
    controlLayout->addWidget(rateButton);
    controlLayout->addWidget(volumeSlider);

    QHBoxLayout *progressLayout = new QHBoxLayout();
    progressLayout->addWidget(currentTimeLabel);
    progressLayout->addWidget(positionSlider);
    progressLayout->addWidget(totalTimeLabel);

    mainLayout->addWidget(videoWidget);
    mainLayout->addLayout(controlLayout);
    mainLayout->addLayout(progressLayout);
    mainLayout->addWidget(videoNameLabel);
    mainLayout->addWidget(playlistWidget);


    connect(playPauseButton, &QPushButton::clicked, this, &MainWindow::playPause);
    connect(muteButton, &QPushButton::clicked, this, &MainWindow::muteUnmute);
    connect(volumeSlider, &QSlider::valueChanged, this, &MainWindow::changeVolume);
    connect(positionSlider, &QSlider::sliderMoved, this, &MainWindow::changePosition);
    connect(nextButton, &QPushButton::clicked, this, &MainWindow::playNext);
    connect(previousButton, &QPushButton::clicked, this, &MainWindow::playPrevious);
    connect(rateButton, &QPushButton::clicked, this, &MainWindow::changePlaybackRate);

    QAction *openFolderAction = new QAction("打开文件夹", this);
    connect(openFolderAction, &QAction::triggered, this, &MainWindow::openFolder);

    QMenu *fileMenu = menuBar()->addMenu("文件");
    fileMenu->addAction(openFolderAction);

    mediaPlayer->setVideoOutput(videoWidget);
    volumeSlider->setRange(0, 100);
    volumeSlider->setValue(50);
    mediaPlayer->setVolume(50);

    // 设置视频小部件的最小大小
    videoWidget->setMinimumSize(640, 480);

}

void MainWindow::loadVideos(const QString &folderPath)
{
    QDir dir(folderPath);
    QStringList filters;
    filters << "*.mp4" << "*.avi" << "*.mkv";
    videoFiles = dir.entryList(filters, QDir::Files);
    playlistWidget->clear();
    for (const QString &file : videoFiles) {
        playlistWidget->addItem(file);
    }
    if (!videoFiles.isEmpty()) {
        currentVideoIndex = 0;
        mediaPlayer->setMedia(QUrl::fromLocalFile(dir.absoluteFilePath(videoFiles[currentVideoIndex])));
        mediaPlayer->play();
    }
}

